const db = require("../db/index");

const overviewController = {
  // 获取产品类别及总价
  getCategoryAndNumber: async (req, res) => {
    const sql = `SELECT product_category AS name, SUM(product_all_price) AS 'value' FROM product GROUP BY product_category`;
    try {
      const [data] = await db.query(sql);
      res.send({
        status: 1,
        message: "成功",
        data: data,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 获取管理员与用户总数
  getAdminAndUserCount: async (req, res) => {
    const sql = `SELECT identity AS 'name', COUNT(*) AS value FROM users GROUP BY identity`;
    try {
      const [data] = await db.query(sql);
      res.send({
        status: 1,
        message: "成功",
        data: data,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 获取消息等级和总数
  getMessageLevelCount: async (req, res) => {
    const sql = `SELECT message_level AS name, COUNT(*) AS value FROM message GROUP BY message_level`;
    try {
      const [data] = await db.query(sql);
      res.send({
        status: 1,
        message: "成功",
        data: data,
      });
    } catch (err) {
      res.cc(err);
    }
  },
  // 每日登录数量
  getloginCount: async (req, res) => {
    const sql = ` SELECT DATE_FORMAT(login_time, '%Y-%m-%d') as name, COUNT(*) as value FROM login_log WHERE login_time >= CURRENT_DATE - INTERVAL 6 DAY GROUP BY DATE_FORMAT(login_time, '%Y-%m-%d') ORDER BY name`;
    try {
      const [data] = await db.query(sql);
      res.send({
        status: 1,
        message: "成功",
        data: data,
      });
    } catch (err) {
      res.cc(err);
    }
  },
};

module.exports = overviewController;
